<script>
// ------- API -------
import { signedAgreements } from '@/api/personnel'

// ------- Utils -------
import { value } from '@/utils/kris_utils'

// ------- Components -------
import List from './list.vue'
import Block from '../Block'


export default {
    components:{
        List,
        Block
    },
    props:{
        userID:{
            required:true,
            default:null
        }
    },
    data(){
        return {
            agreements:[]
        }
    },
    computed:{
        agreementsFormatted(){
            return this.agreements.map(item => {
                const {id,sign_agree,agree:{name}} = item
                return {
                    id,
                    url:sign_agree,
                    name
                }
            })
        }
    },
    methods:{
        async refresh(){
            return await this.initData()
        },
        async initData(){
            try {
                this.loadData()
            }catch(error){
                this.$message.error(error.message)
            }
        },
        async loadData(){
            const result = await Promise.allSettled([
                this.getSignedAgreements()
            ])

            const failTask = result.filter(task => task.status === 'rejected')
            if(failTask.length > 0)
                throw failTask[0].reason
        },
        async getSignedAgreements(){
            let params = {
                user_id:this.userID
            }

            let result = await signedAgreements(params)
                .catch(error => {
                    let errMsg = value(error,'response.data.message','获取已签协议失败')
                    throw new Error(errMsg)
                })

            if(result.code !== 200){
                let errMsg = value(result,'message','获取已签协议失败')
                throw new Error(errMsg)
            }

            // console.log(result)
            this.agreements = result.data
            // this.agreements.push(...result.data)
        }
    },
    created(){
        this.initData()
    }
}
</script>

<template>
    <Block title="已签协议">
        <List :data="agreementsFormatted" />
    </Block>
</template>

<style lang="scss">
</style>